package com.capgemini.diabeo.activity;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.view.View;
import android.widget.ListView;

import com.capgemini.diabeo.R;
import com.capgemini.diabeo.adapter.DishListAdapter;
import com.capgemini.diabeo.carousel.MainCarousel_;
import com.capgemini.diabeo.manager.DishManager;
import com.capgemini.diabeo.manager.MealStatus;
import com.capgemini.diabeo.model.Dish;
import com.capgemini.diabeo.service.DatabaseService;
import com.capgemini.diabeo.service.InitDBService;
import com.capgemini.diabeo.settings.SettingsActivity_;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_main)
public class HomeActivity extends Activity {

	@Bean
	DishListAdapter dishAdapter;

	@ViewById
	ListView dishesList;

	@Bean
	DishManager dishManager;
	
	@Bean
	InitDBService initDBService;

	@AfterViews
	public void afterViews() {
		DatabaseService databaseService = new DatabaseService(this);
		databaseService.createTables();
		long count = databaseService.getCategoryDao().count();
		//Si la BDD est vide
		if(count==0)
		{
			try {
				databaseService.copyDataBase();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			initDBService.initDatabase();
		}
	}

	@Click
	public void mealsButtonClicked(View view) {
		MealChoiceActivity_.intent(this).start();
	}

	@Click
	public void dishesButtonClicked(View view) {
		MainCarousel_.intent(this).start();
	}

	@Click
	public void settingsButtonClicked(View view) {
		SettingsActivity_.intent(this).start();
	}

	@Click
	public void graphiqueButtonClicked(View view) {
		GraphicActivity_.intent(this).start();
	}
	
	@Click
	public void historiqueClicked(View view) {
		HistoriqueMenuActivity_.intent(this).start();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		dishManager.setMenuMeal(MealStatus.DINNER_MENU); //Mettre "autre" ??!
		List<Dish> dishes = dishManager.getMenuDishes();
		dishAdapter.setDishList(dishes);
		dishAdapter.removeAddButton();
	}
}
